//
//  SUXViewController.m
//  SuperCalculatorX
//
//  Created by zhou liangshun on 12-5-26.
//  Copyright (c) 2012年 vistone. All rights reserved.
//

#import "SUXViewController.h"

@interface SUXViewController ()

@end

@implementation SUXViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    sb = [[NSMutableString alloc] initWithCapacity:3];
    lable.textAlignment = UITextAlignmentRight;
    lable.adjustsFontSizeToFitWidth = YES;
    index = 0;
    
	// Do any additional setup after loading the view, typically from a nib.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

-(IBAction)input:(id)sender
{
    UIButton *button = sender;
    char cmd = [button.titleLabel.text characterAtIndex:0];
    switch (cmd) {
        case '0':
        case '1':
        case '2':
        case '3':
        case '4':
        case '5':
        case '6':
        case '7':
        case '8':
        case '9':
            if(index==2)
            {
                sb = [[NSMutableString alloc] init];
                index++;
            }
            [sb appendString:button.titleLabel.text];
            break;
        case '.':
            if([sb rangeOfString:@"."].length==0&&index!=2)
            {
                [sb appendString:@"."];
            }
            break;
        case '+':
        case '-':
        case '*':
        case '/':
            if(index==0)
            {
                strs[index++] = sb;
                strs[index++] = button.titleLabel.text;
            }
        default:
            break;
    }
    
    if([sb length]<1)
        return;

    if([sb rangeOfString:@"."].length==0)
    {
        lable.text = [NSString stringWithFormat:@"%d",[sb intValue]];
        [sb setString:lable.text];
    }else if([sb rangeOfString:@"."].location==sb.length-1)
    {
        NSString *strings = [sb  substringToIndex:sb.length-1];
        lable.text = [NSString stringWithFormat:@"%d",[strings intValue]];
    }
    else
    {
        lable.text = [NSString stringWithFormat:@"%f",[sb doubleValue]];
        [sb setString:lable.text];
    }
    
    //set
};

-(IBAction)process:(id)sender
{
    if(index==3)
    {
        strs[2] = sb;
        double value_1 = [strs[0] doubleValue];
        double value_2 = [strs[2] doubleValue];
        double value;
        switch ([strs[1] characterAtIndex:0]) {
            case '+':
                value = value_1+value_2;
                break;
            case '-':
                value = value_1-value_2;
                break;
            case '*':
                value = value_1*value_2;
                break;
            case '/':
                value = value_1/value_2;
                break;
            default:
                break;
        }
        
        lable.text = [NSString stringWithFormat:@"%f",value];
        
        [sb release];
        sb = [[NSMutableString alloc] initWithCapacity:3];
        
        index = 0;
        
//        [strs[0] release];
//        [strs[1] release];
//        [strs[3] release];
    }
};

-(IBAction)reset:(id)sender
{
    lable.text = @"0";
    
    [sb release];
    sb = [[NSMutableString alloc] initWithCapacity:3];
    
    index = 0;
    
//    [strs[0] release];
//    [strs[1] release];
//    [strs[3] release];
    
}

@end
